// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.inventory;

import java.util.ArrayList;
import zombie.characters.IsoGameCharacter;
import zombie.debug.DebugLog;
import zombie.inventory.types.Clothing;
import zombie.inventory.types.DrainableComboItem;
import zombie.iso.IsoObject;
import zombie.iso.objects.IsoDeadBody;
import zombie.iso.objects.IsoMannequin;
import zombie.iso.objects.IsoWorldInventoryObject;
import zombie.network.GameClient;
import zombie.scripting.ScriptManager;
import zombie.scripting.objects.Item;
import zombie.util.Type;
import zombie.vehicles.VehiclePart;

public final class ItemUser {
    private static final ArrayList<InventoryItem> tempItems = new ArrayList();

    public static void UseItem(InventoryItem item) {
        DrainableComboItem drainableComboItem = Type.tryCastTo(item, DrainableComboItem.class);
        if (drainableComboItem != null) {
            drainableComboItem.setDelta(drainableComboItem.getDelta() - drainableComboItem.getUseDelta());
            if (drainableComboItem.uses > 1) {
                int int0 = drainableComboItem.uses - 1;
                drainableComboItem.uses = 1;
                CreateItem(drainableComboItem.getFullType(), tempItems);
                byte _byte = 0;
                if (_byte < tempItems.size()) {
                    InventoryItem inventoryItem0 = (InventoryItem)tempItems.get(_byte);
                    inventoryItem0.setUses(int0);
                    AddItem(drainableComboItem, inventoryItem0);
                }
            }

            if (drainableComboItem.getDelta() <= 1.0E-4F) {
                drainableComboItem.setDelta(0.0F);
                if (drainableComboItem.getReplaceOnDeplete() == null) {
                    UseItem(drainableComboItem, false, false);
                } else {
                    String string = drainableComboItem.getReplaceOnDepleteFullType();
                    CreateItem(string, tempItems);

                    for (int int1 = 0; int1 < tempItems.size(); int1++) {
                        InventoryItem inventoryItem1 = (InventoryItem)tempItems.get(int1);
                        inventoryItem1.setFavorite(drainableComboItem.isFavorite());
                        AddItem(drainableComboItem, inventoryItem1);
                    }

                    RemoveItem(drainableComboItem);
                }
            }

            drainableComboItem.updateWeight();
        } else {
            UseItem(item, false, false);
        }
    }

    public static void UseItem(InventoryItem item, boolean bCrafting, boolean bInContainer) {
        if (item.isDisappearOnUse() || bCrafting) {
            item.uses--;
            if (item.replaceOnUse != null && !bInContainer && !bCrafting) {
                String string = item.replaceOnUse;
                if (!string.contains(".")) {
                    string = item.module + "." + string;
                }

                CreateItem(string, tempItems);

                for (int _int = 0; _int < tempItems.size(); _int++) {
                    InventoryItem inventoryItem = (InventoryItem)tempItems.get(_int);
                    inventoryItem.setConditionFromModData(item);
                    AddItem(item, inventoryItem);
                    inventoryItem.setFavorite(item.isFavorite());
                }
            }

            if (item.uses <= 0) {
                if (item.keepOnDeplete) {
                    return;
                }

                RemoveItem(item);
            } else if (GameClient.bClient && !item.isInPlayerInventory()) {
                GameClient.instance.sendItemStats(item);
            }
        }
    }

    public static void CreateItem(String fullType, ArrayList<InventoryItem> result) {
        result.clear();
        Item item = ScriptManager.instance.FindItem(fullType);
        if (item == null) {
            DebugLog.General.warn("ERROR: ItemUses.CreateItem: can't find " + fullType);
        } else {
            int int0 = item.getCount();

            for (int int1 = 0; int1 < int0; int1++) {
                InventoryItem inventoryItem = InventoryItemFactory.CreateItem(fullType);
                if (inventoryItem == null) {
                    return;
                }

                result.add(inventoryItem);
            }
        }
    }

    public static void AddItem(InventoryItem existingItem, InventoryItem newItem) {
        IsoWorldInventoryObject isoWorldInventoryObject = existingItem.getWorldItem();
        if (isoWorldInventoryObject != null && isoWorldInventoryObject.getWorldObjectIndex() == -1) {
            isoWorldInventoryObject = null;
        }

        if (isoWorldInventoryObject != null) {
            isoWorldInventoryObject.getSquare().AddWorldInventoryItem(newItem, 0.0F, 0.0F, 0.0F, true);
        } else {
            if (existingItem.container != null) {
                VehiclePart vehiclePart = existingItem.container.vehiclePart;
                if (!existingItem.isInPlayerInventory() && GameClient.bClient) {
                    existingItem.container.addItemOnServer(newItem);
                }

                existingItem.container.AddItem(newItem);
                if (vehiclePart != null) {
                    vehiclePart.setContainerContentAmount(vehiclePart.getItemContainer().getCapacityWeight());
                }
            }
        }
    }

    public static void RemoveItem(InventoryItem item) {
        IsoWorldInventoryObject isoWorldInventoryObject = item.getWorldItem();
        if (isoWorldInventoryObject != null && isoWorldInventoryObject.getWorldObjectIndex() == -1) {
            isoWorldInventoryObject = null;
        }

        if (isoWorldInventoryObject != null) {
            isoWorldInventoryObject.getSquare().transmitRemoveItemFromSquare(isoWorldInventoryObject);
            if (item.container != null) {
                item.container.Items.remove(item);
                item.container.setDirty(true);
                item.container.setDrawDirty(true);
                item.container = null;
            }
        } else {
            if (item.container != null) {
                IsoObject isoObject = item.container.parent;
                VehiclePart vehiclePart = item.container.vehiclePart;
                if (isoObject instanceof IsoGameCharacter isoGameCharacter) {
                    if (item instanceof Clothing) {
                        ((Clothing)item).Unwear();
                    }

                    isoGameCharacter.removeFromHands(item);
                    if (isoGameCharacter.getClothingItem_Back() == item) {
                        isoGameCharacter.setClothingItem_Back(null);
                    }
                } else if (!item.isInPlayerInventory() && GameClient.bClient) {
                    item.container.removeItemOnServer(item);
                }

                item.container.Items.remove(item);
                item.container.setDirty(true);
                item.container.setDrawDirty(true);
                item.container = null;
                if (isoObject instanceof IsoDeadBody) {
                    ((IsoDeadBody)isoObject).checkClothing(item);
                }

                if (isoObject instanceof IsoMannequin) {
                    ((IsoMannequin)isoObject).checkClothing(item);
                }

                if (vehiclePart != null) {
                    vehiclePart.setContainerContentAmount(vehiclePart.getItemContainer().getCapacityWeight());
                }
            }
        }
    }
}
